//
//  AutomationDataManager.h
//  VISIKARD
//
//  Created by Phan Ba Minh on 6/15/12.
//  Copyright (c) 2012 kidbaw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Define.h"
#import "APIRequester.h"
#import "CoreLocation/CoreLocation.h"

#define TIMER_AUTOMATION_UPDATING                                           10
#define TIMER_AUTOMATION_CHECKING_NETWORK                                   30
#define TIMER_LOCATION_ALLOW_TO_UPDATE                                      10

typedef enum {
    enumAutomationDataManagerStep_Invalid,
    // Update user location
    enumAutomationDataManagerStep_UpdateUserLocation,
    enumAutomationDataManagerStep_UpdateUserLocation_Waiting,
    enumAutomationDataManagerStep_UpdateUserLocation_Finished,
    enumAutomationDataManagerStep_UpdateUserLocation_Failed,
    enumAutomationDataManagerStep_UpdateUserLocation_Timeout,
//    // Update user online status
//    enumAutomationDataManagerStep_UpdateUserOnlineStatus,
//    enumAutomationDataManagerStep_UpdateUserOnlineStatus_Waiting,
//    enumAutomationDataManagerStep_UpdateUserOnlineStatus_Finished,
//    enumAutomationDataManagerStep_UpdateUserOnlineStatus_Failed,
//    enumAutomationDataManagerStep_UpdateUserOnlineStatus_Timeout,
    // 
    enumAutomationDataManagerStep_CheckingForNext,
    enumAutomationDataManagerStep_CheckingForNetwork,
    enumAutomationDataManagerStep_End
    
}enumAutomationDataManagerStep;

typedef enum {
    enumAutomationDataIsReady_Invalid,
    enumAutomationDataIsReady_ForHome_BeforeEntering,
    enumAutomationDataIsReady_ForHome_ReloadingHomeImage,
    enumAutomationDataIsReady_ForHome_ReloadingHomeImage_Shaking,
    enumAutomationDataIsReady_ForRegister_CreatingAccount,
    enumAutomationDataIsReady_ForKardBuilder_SavingKard,
    enumAutomationDataIsReady_ForSimpleKard_DeletingKard,
    enumAutomationDataIsReady_ForSimpleKard_MakingDefaultKard,
    enumAutomationDataIsReady_ForSimpleKard_ApprovingKard,
    enumAutomationDataIsReady_ForKonnectSetting_SettingDefaultKard,
    enumAutomationDataIsReady_ForKonnect_UpdateUserLocation,
    enumAutomationDataIsReady_ForKonnect_GetPendingKards,
    enumAutomationDataIsReady_ForAutomation
}enumAutomationDataIsReady;


typedef enum {
    enumLocationState_Invalid,
    enumLocationState_Standard_Running,
    enumLocationState_Standard_Stop,
    enumLocationState_SLC_Running,
    enumLocationState_SLC_Stop,
    enumLocationState_Stop,
}enumLocationState;

 
@protocol AutomationDataManagerProtocol <NSObject> 
@optional
- (void)didRequestFinishedWithDataIsReady:(enumAutomationDataIsReady)isReady;
- (void)didRequestFailedWithDataIsReady:(enumAutomationDataIsReady)isReady;

@end

@interface AutomationDataManager : NSObject <AppViewControllerProtocol, APIRequesterProtocol, UIAlertViewDelegate, CLLocationManagerDelegate> {
    enumAutomationDataManagerStep           _automationDataManagerStep;
    enumAutomationDataManagerStep           _previousStep;
    
    CFAbsoluteTime                          _requestTimer;
    APIRequester                            *_APIRequester;
    CFAbsoluteTime                          _checkingNetworkTimer;
    BOOL                                    _alertCheckingNetwork_Showing;
    
    CLLocationManager                       *_locationManager;
    
    enumAutomationDataIsReady               _dataIsReady;
    id<AutomationDataManagerProtocol>       _delegate;
    
    APIRequester                            *_APIRequesterKardBuilder;
    int                                     _pageIndexTemplate;
    int                                     _pageIndexGraphic;
    
    UIAlertView                             *_alertLocationService;
    
    UIAlertView                             *_alertRequestFailed;
    
    int                                     _pageIndexMeKards;
    UIBackgroundTaskIdentifier              bgTask;
    enumLocationState                       _locationState;
    NSDate                                  *_locationTimeOut;
    BOOL                                    _isFirstTime;
	
}
- (void)cancelRequest;

- (void)reloadAllDataWithDataIsReady:(enumAutomationDataIsReady)isReady andDelegate:(id<AutomationDataManagerProtocol>)delegate;

//- (void)updateUserLocationService;
- (BOOL)isValidLocationService;
- (void)startUpdateLocationService;
- (void)stopUpdateLocationService;
- (void)startMonitoringSignificantLocationChanges;
- (void)stopMonitoringSignificantLocationChanges;

+ (AutomationDataManager *)Shared;

@end
